package twentytwenty;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2020-12-12 13:41
 * @Modified By:
 */
public class Intersection_349 {
    public static void main(String[] args) {
        int [] nums1 = new int[]{4,9,5};
        int [] nums2 = new int[]{9,4,9,8,4};
        System.out.println(Arrays.toString(intersection(nums1, nums2)));
    }

    public static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> nums1Set = new HashSet<>();
        Set<Integer> result = new HashSet<>();
        for (int val : nums1) {
            if (!nums1Set.contains(val)){
                nums1Set.add(val);
            }
        }
        for (int j : nums2) {
            if (nums1Set.contains(j) && !result.contains(j)) {
                result.add(j);
            }
        }
        Object[] array = result.toArray();
        int [] arr = new int[array.length];
        int i = 0;
        for (Object o : array) {
            arr[i] = (int) o;
            i++;
        }
        return arr;
    }
}
